package com.ht.medicaltreatment.viewmodel

import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnHttpListener
import com.ht.medicaltreatment.bean.ConsumableLeftListBean
import com.ht.medicaltreatment.bean.ConsumableLeftRightBean
import com.ht.medicaltreatment.bean.ConsumableListBean
import com.ht.medicaltreatment.bean.ConsumableListData
import com.ht.medicaltreatment.bean.GuiGeBean
import com.ht.medicaltreatment.bean.GuiGeData
import com.ht.medicaltreatment.bean.SubjectListBean
import com.ht.medicaltreatment.bean.SubjectListData
import com.ht.medicaltreatment.bean.SubjectRecordBean
import com.ht.medicaltreatment.bean.SubjectRecordData
import com.ht.medicaltreatment.bean.SubmitConsumableJSON
import com.ht.medicaltreatment.bean.XingHaoBean
import com.ht.medicaltreatment.http.api.CaiZhiAPI
import com.ht.medicaltreatment.http.api.ConsumableListAPI
import com.ht.medicaltreatment.http.api.GuiGeAPI
import com.ht.medicaltreatment.http.api.PingPaiAPI
import com.ht.medicaltreatment.http.api.SubjectListAPI
import com.ht.medicaltreatment.http.api.SubjectRecordAPI
import com.ht.medicaltreatment.http.api.SubjectRightListAPI
import com.ht.medicaltreatment.http.api.XingHaoAPI

class ConsumableViewModel : BaseViewModel() {

    fun init(projectId:Int,OnCallBack:(SubjectRecordData)->Unit){
        EasyHttp.post(this)
            .api(SubjectRecordAPI(projectId))
            .request(object : OnHttpListener<SubjectRecordBean>{
                override fun onHttpSuccess(result: SubjectRecordBean) {
                    if(result.code == 200){
                        OnCallBack(result.data)
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception?) {
                    dismissProgress(e?.message?:"请求错误")
                }
            })
    }

    //耗材明细
    fun consumableList(projectId:Int,OnCallBack:(ConsumableListData)->Unit){
        EasyHttp.post(this)
            .api(ConsumableListAPI(projectId))
            .request(object : OnHttpListener<ConsumableListBean>{
                override fun onHttpSuccess(result: ConsumableListBean) {
                    if(result.code == 200){
                        OnCallBack(result.data)
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception?) {
                    dismissProgress(e?.message?:"请求错误")
                }
            })
    }

    fun subjectList(page:Int,protocolNumber:String,OnCallBack:(SubjectListData)->Unit){
        EasyHttp.post(this)
            .api(SubjectListAPI(currentPage = page,protocolNumber = protocolNumber))
            .request(object : OnHttpListener<SubjectListBean>{
                override fun onHttpSuccess(result: SubjectListBean) {
                    if(result.code == 200){
                        OnCallBack(result.data)
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception?) {
                    dismissProgress(e?.message?:"请求错误")
                }
            })
    }

    fun consumableList(onSuccess:(MutableList<ConsumableLeftListBean.DataBean>)->Unit){
        EasyHttp.post(this)
            .api("/other/app_consumable/findConsumableTypeList.do")
            .request(object : OnHttpListener<ConsumableLeftListBean>{
                override fun onHttpSuccess(result: ConsumableLeftListBean) {
                    if(result.code == 200){
                        onSuccess(result.data)
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception?) {
                    dismissProgress(e?.message?:"请求错误")
                }
            })
    }

    fun consumableListRight(sign:Int,onSuccess:(MutableList<String>)->Unit){
        EasyHttp.post(this)
            .api(SubjectRightListAPI(sign))
            .request(object : OnHttpListener<ConsumableLeftRightBean>{
                override fun onHttpSuccess(result: ConsumableLeftRightBean) {
                    if(result.code == 200){
                        onSuccess(result.data)
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception?) {
                    dismissProgress(e?.message?:"请求错误")
                }
            })
    }

    //型号
    fun xinghao(sign:Int,name:String,onSuccess:(MutableList<String>)->Unit){
        EasyHttp.post(this)
            .api(XingHaoAPI(sign,name))
            .request(object : OnHttpListener<XingHaoBean>{
                override fun onHttpSuccess(result: XingHaoBean) {
                    if(result.code == 200){
                        onSuccess(result.data)
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception?) {
                    dismissProgress(e?.message?:"请求错误")
                }
            })
    }

    //规格
    fun guige(sign:Int,name:String,xinghao:String,onSuccess:(MutableList<GuiGeData>)->Unit){
        EasyHttp.post(this)
            .api(GuiGeAPI(sign,name,xinghao))
            .request(object : OnHttpListener<GuiGeBean>{
                override fun onHttpSuccess(result: GuiGeBean) {
                    if(result.code == 200){
                        onSuccess(result.data)
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception?) {
                    dismissProgress(e?.message?:"请求错误")
                }
            })
    }

    //品牌
    fun pinpai(sign:Int,name:String,xinghao:String,guige:String,onSuccess:(MutableList<GuiGeData>)->Unit){
        EasyHttp.post(this)
            .api(PingPaiAPI(sign,name,guige,xinghao))
            .request(object : OnHttpListener<GuiGeBean>{
                override fun onHttpSuccess(result: GuiGeBean) {
                    if(result.code == 200){
                        onSuccess(result.data)
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception?) {
                    dismissProgress(e?.message?:"请求错误")
                }
            })
    }

    //材质
    fun caizhi(sign:Int,name:String,xinghao:String,guige:String,pinpai:String,onSuccess:(MutableList<GuiGeData>)->Unit){
        EasyHttp.post(this)
            .api(CaiZhiAPI(sign,name,guige,pinpai,xinghao))
            .request(object : OnHttpListener<GuiGeBean>{
                override fun onHttpSuccess(result: GuiGeBean) {
                    if(result.code == 200){
                        onSuccess(result.data)
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception?) {
                    dismissProgress(e?.message?:"请求错误")
                }
            })
    }

    fun submit(bean: SubmitConsumableJSON,onSuccess:()->Unit){
        EasyHttp.post(this)
            .api(bean)
            .request(object : OnHttpListener<GuiGeBean>{
                override fun onHttpSuccess(result: GuiGeBean) {
                    if(result.code == 200){
                        onSuccess()
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception?) {
                    dismissProgress(e?.message?:"请求错误")
                }
            })
    }
}